<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Admin\Category;
use App\Models\Admin\Article;
use App\Models\Admin\About;
use App\Models\Admin\People;
use DB;
use Auth;

use Intervention\Image\ImageManagerStatic as Image;

class ArticleController extends Controller
{
    //文章列表展示
    public function index(Request $request){

    	$data = Category::get();
    	$cate = new Category();
    	$result = $cate ->categoryList($data);
    	$title = $request->input('title') ? $request->input('title'):'' ;
    

    	//select wt_new.*, wt_form.name, manager.username from wt_new left join wt_form on wt_new.category_id= wt_form.id left join manager on wt_new.user_id = manager.id;

    	$artdata =DB::table('wt_new')->select('wt_new.*', 'wt_form.name', 'manager.username')->leftJoin('wt_form','wt_new.category_id','=','wt_form.id')->leftJoin('manager','wt_new.user_id','=','manager.id')->get();

    	
    	return view('admin.article.index',compact('result','artdata','title'));
    }
    //文章添加
    public function articleadd(Request $request){
    	if ($request->isMethod('post')) {

    		 // var_dump(Auth::user());die;
    			 		
    		$result = Article::insert([
            'title'  => $request->input('title'),
	    	'category_id'  => $request->input('category_id'),
	    	'user_id'  =>  Auth::user()->id,
	    	'sort'     =>$request->input('sort'),
            'keywords'     =>$request->input('keywords'),
            'description'     =>$request->input('description'),
            'author'=> $request->input('author'),
            'top'=> $request->input('top'),

	    	'contents'=> $request->input('contents'),

	    	'time' =>time(),
            'post' => '1',
            'thumb' => $request->input('avatar'),
	    	]);
	    	return $result ? '1' : '0'; 
    	}else{

	    	$data = Category::get();
	    	$cate = new Category();
	    	$result = $cate ->categoryList($data);
	    	
	    	return view('admin.article.articleadd',compact('result'));

    	}

    }
    //文章搜索
    public function articlesearch(Request $request){
    	if ($request->isMethod('get')) {
    		 		
	    	$data = Category::get();
	    	$cate = new Category();
	    	$result = $cate ->categoryList($data);
            $title = $request->input('title')? $request->input('title'):'' ;
	    	

	    	//select wt_new.*, wt_form.name, manager.username from wt_new left join wt_form on wt_new.category_id= wt_form.id left join manager on wt_new.user_id = manager.id;

	    	$artdata =DB::table('wt_new')->where('title','like','%'.$request->input('title').'%')->select('wt_new.*', 'wt_form.name', 'manager.username')->leftJoin('wt_form','wt_new.category_id','=','wt_form.id')->leftJoin('manager','wt_new.user_id','=','manager.id')->get();  	

	    	return view('admin.article.index',compact('result','artdata','title'));

    	}

    }

    //文章修改
    public function articleedit(Request $request){
    	if ($request->isMethod('post')) {

    		 		
    		$result = Article::where('id',$request->input('id'))->update([

	    	'title'  => $request->input('title'),
            'category_id'  => $request->input('category_id'),
            //'user_id'  =>  Auth::user()->id,
            'sort'     =>$request->input('sort'),
            'keywords'     =>$request->input('keywords'),
            'description'     =>$request->input('description'),
            'author'=> $request->input('author'),
            'top'=> $request->input('top'),

            'contents'=> $request->input('contents'),

            'thumb' => $request->input('avatar'),
	    	
	    	]);
	    	return $result ? '1' : '0'; 
    	}else{

    		$article = Article::where('id',$request->input('id'))->get();
    		$datas = Category::where('id',$request->input('id'))->get();
	    	$data = Category::get();
	    	$cate = new Category();
	    	$result = $cate ->categoryList($data);
	    	
	    	return view('admin.article.articleedit',compact('result','article','datas'));

    	}

    }

    //文章删除
    public function articledel(Request $request){

        $result = Article::where('id',$request->only('id'))->delete();
        return $result ? '1' : '0'; 

    }
    //文章预览
     public function preview(Request $request){

     	$article = Article::where('id',$request->input('id'))->get();

     	return view('admin.article.preview',compact('article'));


     }



    //显示分类信息
    public function category(){
    	$data = Category::get();
    	$cate = new Category();
    	$result = $cate ->categoryList($data);
    	// var_dump($result);

    	return view('admin.article.category',compact('result'));
    }

     //添加分类信息
    public function categoryadd(Request $request){

    	if ($request->isMethod('post')) {
    		$data = $request->except('_token');
    		$result = Category::insert($data);
    		return $result ? '1' : '0'; 
    	}else{

    	$data = Category::get();
    	$cate = new Category();
    	$result = $cate ->categoryList($data);

    	return view('admin.article.categoryadd',compact('result'));
    	}

    }

    //删除分类信息
     public function categorydel(Request $request){

    	if ($request->isMethod('get')) {

            $res = Category::where('pid',$request->only('id'))->get();
            if (!$res->isEmpty()) {      
               
                return  '0'; 
            }else{
             
             $result = Category::where('id',$request->only('id'))->delete();
             return $result ? '1' : '0'; 

            }
           
    		
    	
    	}

    }
    //分类更新
    public function categoryedit(Request $request){

    	if ($request->isMethod('post')) {
    		
    		$result = Category::where('id',$request->input('id'))->update([
    			'id' => $request->input('id'),
    			'name' => $request->input('name'),
    			'sort' => $request->input('sort'),
    			'pid'  => $request->input('pid'),

    		]);
    		return $result ? '1' : '0'; 
    	}else{

    		$datas = Category::where('id',$request->only('id'))->get();
    		$data = Category::get();
	    	$cate = new Category();
	    	$result = $cate ->categoryList($data);

    		return view('admin.article.categoryedit',compact('datas','result'));
    	}

    }

     //下架信息
    public function stop(Request $request){
       

        $data = Article::where('id',$request->only('id'))->update(['post' => '2']);
    }

    //发布信息
    public function start(Request $request){
        

        $data = Article::where('id',$request->only('id'))->update(['post' => '1']);
    }

    //新闻缩略图
    public function addArticleDeal(Request $request)
    {
        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            $photo = $request->file('file');
            $file_name = uniqid().'.'.$photo->getClientOriginalExtension();
            $file_path =public_path('storage');
            $thumbnail_file_path = $file_path . '\thumbnail-'.$file_name;
            $image = Image::make($photo)->resize(97, 75, function ($constraint) {$constraint->aspectRatio();})->save($thumbnail_file_path);
            //echo '上传成功';
      
            $result = [ 
                'errCode'  => '0',
                'errMsg'   => '',
                'succMsg'  => '文件上传成功！',
                'path'     => '/storage/thumbnail-'.$file_name,
             
            ];
        }
        //exit('未获取到上传文件或上传过程出错');
        else {
            $result = [
                'errCode'  => '0000001',
                'errMsg'   => $request ->file('file')->getErrorMessage(),

            ];
           
        }
        return response()-> json($result);


    }

    public function about(){

        $data = About::get();
        return view('admin.about.about',compact('data'));
    }

    public function aboutadd(Request $request){

        if ($request->isMethod('post')) {
           
           $result = About::insert([

            'name'  => $request->input('title'),
           
            'sort'  => $request->input('sort'),
            'contents'=> $request->input('contents'),
        
            ]);
            return $result ? '1' : '0'; 

        }else{
            
            return view('admin.about.aboutadd');
            
        }
    }
    public function aboutedit(Request $request){

        if ($request->isMethod('post')) {

            $result = About::where('id',$request->only('id'))->update([

            'name'  => $request->input('title'),
           
            'sort'  => $request->input('sort'),
            'contents'=> $request->input('contents'),
        
            ]);
            return $result ? '1' : '0'; 

        }else{
            $data = About::where('id',$request->only('id'))->get();
             
            return view('admin.about.aboutedit',compact('data'));

        }
    }
    public function people(){

        
        $data = People::get();
        return view('admin.people.index',compact('data'));
    }

    public function peopleadd(Request $request){

        if ($request->isMethod('post')) {
           
           $result = People::insert([

            'name'  => $request->input('title'),
           
            'sort'  => $request->input('sort'),
            'contents'=> $request->input('contents'),
        
            ]);
            return $result ? '1' : '0'; 

        }else{
            
            return view('admin.people.peopleadd');
            
        }
    }

    public function peopleedit(Request $request){

        if ($request->isMethod('post')) {

            $result = People::where('id',$request->only('id'))->update([

            'name'  => $request->input('title'),
           
            'sort'  => $request->input('sort'),
            'contents'=> $request->input('contents'),
        
            ]);
            return $result ? '1' : '0'; 

        }else{
            $data = People::where('id',$request->only('id'))->get();
             
            return view('admin.people.peopleedit',compact('data'));

        }
    }


  

    

}
